﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.IO;
using System.Data;

namespace FormBase
{
    /// <summary>
    /// To maintain text file in format of (key=value... list) and UTF-8
    /// Note: It's not an .ini file.
    /// </summary>
    public class CDictionaryFile
    {
        string msMsg;
        //string msFileFullPath;
        public readonly Dictionary<string, string> mDict = new Dictionary<string, string>();
        Dictionary<string, string> mDictBackup = new Dictionary<string, string>();
        public CDictionaryFile()
        {
            //msFileFullPath = Path.GetFullPath(sFile);
        }
        public string Message { get { return msMsg; } }
        public string FilePath { get; set; }
        public bool LoadFile()
        {
            string sLine, sKey, sValue;
            string[] sa1;
            char[] caSeparator = new char[] { '=' };

            if (!File.Exists(FilePath))
            { 
                msMsg = "File not found." + FilePath;
                return false;
            }

            //            int iLenKey, iLenValue;
            using (StreamReader sr = new StreamReader(FilePath, Encoding.UTF8))
            {
                while (!sr.EndOfStream)
                {
                    sLine = sr.ReadLine();
                    sa1 = sLine.Split(caSeparator, 2); // only left and right string.
                    sKey = sa1[0];
                    if (ExistKey(sKey))
                    {
                        sValue = sa1[1];
                        SetValue(sKey, sValue);
                    }
                }
            }

            mDictBackup.Clear();
            foreach (KeyValuePair<string, string> kvp in mDict)
                mDictBackup.Add(kvp.Key, kvp.Value);
            
            return true;
        }
        public bool IsChanged()
        {
            string sValue;
            foreach (KeyValuePair<string, string> kvp in mDict)
            {
                if (!mDictBackup.TryGetValue(kvp.Key, out sValue))
                    return true;

                if (String.Compare(kvp.Value, sValue) != 0)
                    return true;
            }
            return false;
        }

        public void SaveFile()
        {

        }
        public bool ExistKey(string sKey)
        {
            if (string.IsNullOrEmpty(sKey))
                return false;

            return mDict.ContainsKey(sKey);
        }
        public bool GetValue(string sKey, out string sValue)
        {
            //if (string.IsNullOrEmpty(sKey))
            //    return false;

            //if (!ExistKey(sKey))
            //{
            //    sValue = string.Empty;
            //    return false;
            //}
            //sValue = mDict[sKey];
            //return true;
            return mDict.TryGetValue(sKey, out sValue);
        }
        public bool SetValue(string sKey, string sValue)
        {
            if (string.IsNullOrEmpty(sKey))
                return false;

            if (!mDict.ContainsKey(sKey))
                return false;

            mDict[sKey] = sValue;
            return true;
        }
        public bool AddValue(string sKey, string sValue)
        {
            if (string.IsNullOrEmpty(sKey))
                return false;

            if (ExistKey(sKey))
                return false;

            mDict.Add(sKey, sValue);
            return true;
        }
        public string[] GetKeys()
        {
            string[] sa1 = new string[mDict.Count];
            int i;
            i = 0;
            foreach (KeyValuePair<string, string> kvp in mDict)
            {
                //Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
                sa1[i] = kvp.Key;
                i++;
            }

            return sa1;

        }
        private DataSet xxGetDataSet()
        {
            DataTable TDict = myCreateTableDictionary();
            DataSet dsDict = new DataSet();
            dsDict.Tables.Add(TDict);

            DataRow dr1;
            foreach (KeyValuePair<string, string> kvp in mDict)
            {
                dr1 = TDict.NewRow();
                dr1[0] = kvp.Key;
                dr1[1] = kvp.Value;
                TDict.Rows.Add(dr1);
            }
            return dsDict;
        }
        public DataTable GetDataTable()
        {
            DataTable table1 = myCreateTableDictionary();

            DataRow row1;
            foreach (KeyValuePair<string, string> kvp in mDict)
            {
                row1 = table1.NewRow();
                row1[0] = kvp.Key;
                row1[1] = kvp.Value;
                table1.Rows.Add(row1);
            }
            return table1;
        }
        private DataTable myCreateTableDictionary()
        {
            DataTable table1 = new DataTable("TDictionary");
            DataColumn[] cols = {
                                    new DataColumn("FKey", typeof(String)),
                                    new DataColumn("FValue", typeof(String))
                                };
            table1.Columns.AddRange(cols);
            table1.PrimaryKey = new DataColumn[] { table1.Columns["FKey"] };
            return table1;
        }


    }
}
